package com.lai.lai.service.impl;

import com.lai.lai.dao.AreaDao;
import com.lai.lai.entity.Area;
import com.lai.lai.enums.EmBusiness;
import com.lai.lai.utils.ResultResponse;
import com.lai.lai.service.AreaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lmx
 * @Date 2021/4/27 21:23
 * @Version 1.0
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Resource
    private AreaDao areaDao;
    @Override
    public ResultResponse<List<Area>> queryArea() {
        return ResultResponse.ok(areaDao.queryArea());
    }

    @Override
    public ResultResponse<Area> queryAreaById(Integer areaId) {
        Area area = areaDao.queryAreaById(areaId);
        return ResultResponse.ok(area);
    }

    @Transactional
    @Override
    public ResultResponse<Area> insertArea(String areaName,Integer priority) {
        Area area = new Area();
        if (areaName != null && !"".equals(areaName) && priority > 0){
            area.setAreaName(areaName).setPriority(priority);
            area.setCreateTime(new Date());
            area.setLastTime(new Date());
            try{
                int isSuccess = areaDao.insertArea(area);
                if (isSuccess > 0){
                    return ResultResponse.ok(area);
                }else {
                    return ResultResponse.fail(EmBusiness.AREA_INSERT_ERROR.getCode(),EmBusiness.AREA_INSERT_ERROR.getMsg());
                }
            }catch (Exception e){
                return ResultResponse.fail(EmBusiness.UNKNOWN_ERROR.getCode(), EmBusiness.UNKNOWN_ERROR.getMsg());
            }
        }else{
            return ResultResponse.fail(EmBusiness.PARAMETER_VALIDATION_ERROR.getCode(), EmBusiness.PARAMETER_VALIDATION_ERROR.getMsg());
        }
    }

    @Override
    public ResultResponse<Area> updateArea(Integer areaId,String areaName,Integer priority) {
        Area area = areaDao.queryAreaById(areaId);
        if (!areaDao.findId(areaId)){
            return ResultResponse.fail();
        }
        if (areaDao.findId(areaId)){
            String name = area.getAreaName();
            if (areaName != null){
                name = areaName;
                area.setAreaName(areaName);
            }
            if (priority == null)priority = area.getPriority();
            else area.setPriority(priority);
            try{
                int isSuccess = areaDao.updateArea(areaId,name,priority,new Date());
                if (isSuccess > 0){
                    return ResultResponse.ok(area);
                }else {

                    return ResultResponse.fail(EmBusiness.AREA_UPDATE_ERROR.getCode(), EmBusiness.AREA_UPDATE_ERROR.getMsg());
                }
            }catch (Exception e){
                e.printStackTrace();
                return ResultResponse.fail(EmBusiness.UNKNOWN_ERROR.getCode() ,EmBusiness.UNKNOWN_ERROR.getMsg());
            }
        }else{
            return ResultResponse.fail(EmBusiness.PARAMETER_VALIDATION_ERROR.getCode() ,EmBusiness.PARAMETER_VALIDATION_ERROR.getMsg());
        }
    }

    @Override
    public ResultResponse<Integer> deleteArea(Integer id) {
        if (id != null && id > 0){
            try{
                int isSuccess = areaDao.deleteArea(id);
                if (isSuccess > 0){
                    return ResultResponse.ok(id);
                }else {
                    return ResultResponse.fail(EmBusiness.AREA_DELETE_ERROR.getCode(), EmBusiness.AREA_DELETE_ERROR.getMsg());
                }
            }catch (Exception e){
                return ResultResponse.fail(EmBusiness.PARAMETER_VALIDATION_ERROR.getCode(), EmBusiness.PARAMETER_VALIDATION_ERROR.getMsg());
            }
        }else{
            return ResultResponse.fail(EmBusiness.PARAMETER_VALIDATION_ERROR.getCode() ,EmBusiness.PARAMETER_VALIDATION_ERROR.getMsg());
        }
    }
}
